#include "transferform.h"
#include "ui_transferform.h"
#include "moveableframelessdialog.h"
#include <QProgressBar>
#include <QDebug>
#include "../model/transferfromqthread.h"

transferForm::transferForm(QWidget *parent) :
    MoveableFramelessDialgo(parent),
    ui(new Ui::transferForm)
{
    ui->setupUi(this);


    //关闭
    connect(ui->pushButton_close,SIGNAL(clicked()),this,SLOT(doClose()));
}

transferForm::~transferForm()
{
    delete ui;
}
void transferForm::onThemeColorChange(QString colorStr)
{
    QString style=QString("#%1{background-color:%2}")
            .arg(ui->centralwidget->objectName(),colorStr);

    ui->centralwidget->setStyleSheet(style);

}
void transferForm::doClose()
{
    this->close();
}
QWidget *transferForm::getDragnWidget()
{
    return ui->transfer_main_widget;
}
void transferForm::addItem(int tfd,int ffd,int magic,int maxPageNum,QString label)
{
    QWidget *w = new QWidget;
    QVBoxLayout *vlayout=new QVBoxLayout;
    QProgressBar *nProgressBar=new QProgressBar(w);
    QString objName=QString("%1.%2.%3").arg(tfd).arg(ffd).arg(magic);
    QProgressBar *bar=ui->listWidget->findChild<QProgressBar *>(objName);
    if(bar!=NULL) return;
    //qDebug()<<"添加传送"<<objName;
    nProgressBar->setObjectName(objName);
    if(maxPageNum>1)
    nProgressBar->setMaximum(maxPageNum);

    nProgressBar->setFormat(label+"  %p% ");
    vlayout->addWidget(nProgressBar);
    w->setLayout(vlayout);

    QListWidgetItem *litem = new QListWidgetItem;
    litem->setSizeHint(QSize(w->size().width(),33));
    ui->listWidget->addItem(litem);
    ui->listWidget->setItemWidget(litem,w);

}
void transferForm::updateProgress(int tfd,int ffd, int magic,int index)
{
    QString objName=QString("%1.%2.%3").arg(tfd).arg(ffd).arg(magic);
    //qDebug()<<"更新传送"<<objName;
    QProgressBar *bar=ui->listWidget->findChild<QProgressBar *>(objName);
    if(bar!=NULL)
    {
        if(bar->maximum()<index)
            bar->setMaximum(index);
        bar->setValue(index);
        bar->setMinimum(0);
    }

}
void transferForm::removeItem(int tfd, int ffd, int magic)
{
    QString objName=QString("%1.%2.%3").arg(tfd).arg(ffd).arg(magic);
    //qDebug()<<"更新传送"<<objName;
    QProgressBar *bar=ui->listWidget->findChild<QProgressBar *>(objName);
    if(bar!=NULL)
    {

        int n=ui->listWidget->count();//获取item的总数
            //删去所有item
            for(int i=0;i<n;i++)
            {
                /*
                 使用takeItem(row)函数将QListWidget中的第row个item移除，
                 移除需要使用delete手动释放其在堆上占用的空间
                */
                QListWidgetItem *item = ui->listWidget->item(i);
                if(bar->parentWidget()==ui->listWidget->itemWidget(item))
                {
                    ui->listWidget->takeItem(i);
                    delete bar;
                    delete item;
                    break;
                }

            }
    }
}
void transferForm::setTitle(QString title)
{
    QString text=QString("%1 传送列表").arg(title);
    ui->label_title->setText(text);
}

void transferForm::beginTransfer(int index)
{

    //局部线程的创建的创建
    TransferFromQThread* thread = new TransferFromQThread(WSIM_client::getLinkUp(index),NULL);//这里父对象指定为NULL
    //connect(thread,&TransferFromQThread::message            ,this,&transferForm::receiveMessage);
    connect(thread,&TransferFromQThread::progress            ,this,&transferForm::updateProgress);
    connect(thread,&QThread::finished            ,this,&transferForm::onQThreadFinished);
    connect(thread,&QThread::finished            ,thread,&QObject::deleteLater);//线程结束后调用deleteLater来销毁分配的内存
    thread->start();


}
void transferForm::onQThreadFinished()
{
    qDebug()<<("TransferFromQThread finish");
}
